<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Api_V2_Member
{


    /**
     * @function 统计会员相关模块数量
     * @param $mid
     * @return array
     */
    public static function calculate_num($mid)
    {
        $out = array();

        $member = DB::select()->from('member')->where('mid', '=', $mid)->execute()->current();
        $money  = bcsub($member['money'],$member['money_frozen'],2);
        if(St_Functions::is_normal_app_install('system_wallet')){
            // 余额
            $wallet_member = DB::select()->from('wallet_member')->where('member_id', '=', $mid)->execute()->current();
            $out['wallet_balance'] = $wallet_member['remain_amount'];
            $out['total_balance']  = bcadd($money, $out['wallet_balance'], 2);
        }else{
            $out['wallet_balance'] = 0;
            $out['total_balance']  = $money;
        }
        // 积分
        $out['jifen'] = empty($member['jifen']) ? 0 : $member['jifen'];
        // 会员等级
        $grade = Model_Member::member_rank($mid, array('return' => 'all'));
        $out['rank'] = $grade['current'];
        //权益数量
        $equity_id = DB::select()->from('member_grade_equity')->where('grade_id', '=', $grade['current_rank']['id'])->execute()->get('equity_id');
        $out['equity_num'] = count(explode(',',$equity_id));
        // 旅游卡数量
        $out['card_num'] = 0;
        if (St_Functions::is_normal_app_install('system_card')) {
            $out['card_num'] = DB::select()
                ->from(['card_member','a'])
                ->join(['member_order','b'],'left')
                ->on('a.order_id','=',DB::expr("sline_b.id"))
                ->where('a.member_id', '=', $mid)
                ->and_where('b.status','in',[2,5])
                ->execute()->count();
        }

        // 钱包
        $money = $member['money'] ? $member['money'] : 0;
        $money_frozen = $member['money_frozen'] ? $member['money_frozen'] : 0;
        $out['money'] = $money - $money_frozen;

        //优惠券数量
        if (St_Functions::is_normal_app_install('coupon')) {
            $out['coupon'] = DB::select('id')
                ->from('member_coupon')
                ->where('mid', '=', $mid)
                ->where('usenum', '=', 0)
                ->execute()
                ->count();
        }

        //红包
        if (St_Functions::is_normal_app_install('red_envelope')) {
            $out['red_envelope'] = DB::select('id')->from('envelope_member')
                ->where('memberid', '=', $mid)->and_where('use', '=', 0)
                ->execute()->count();
        }

        //我的游记数量
        if (St_Functions::is_system_app_install(101)) {
            $out['notes'] = DB::select('id')->from('notes')->where('memberid', '=', $mid)->execute()->count();
        }

        //我的结伴数量
        if (St_Functions::is_system_app_install(11)) {
            $out['jieban'] = DB::select('id')->from('jieban')->where('memberid', '=', $mid)->execute()->count();
        }

        //我的咨询
        $out['question'] = DB::select('id')->from('question')->where('memberid', '=', $mid)->execute()->count();

        return $out;
    }
}